clear; clc;
% Subjects;
% load(subject.CW.EpochData.SphereAmbiguousResponse.TfcDatafile);
% load(subject.CW.EpochData.SphereUnambiguousResponse.TfcDatafile);
% load('CW_Chanlocs.mat');
% preselection = {'P10','OcO4'};%'T18', 'T25', 'sTa3', 'OcO2','OcO3','OcO4','OcO5','OcO9','Oc10','Oc11','T09','T07','P02','P09','P10'

load('D:\ECoG\Data\Timefreq\TfcForICA\CW SphereUnambiguousResponse TfcForICA')
data = tfc.data.P10;
data = data(1:10,:);

% for i = 1:length(preselection)
%     psIdx(i) = find(strcmp(preselection{i},{CW_Chanlocs.labels}));
% end;
% selectedChanlocs = CW_Chanlocs(psIdx);
% % clf;
% % hold
% % for i = 1:length(preselection)
% %     plot(tfc.times, tfc.data.(preselection{i})(3).mean);
% % end
% 
% data = nan(length(preselection)*5,length(tfc.times));
% desc = cell(length(preselection)*5,1);
% for i = 1:length(preselection)
%     for j = 1:length(tfc.data.(preselection{i}))
%         data((i-1)*5 + j, :) = tfc.data.(preselection{i})(j).mean;
%         frequencyrange = tfc.data.(preselection{i})(j).frequencyrange;
%         desc{(i-1)*5 + j} = [preselection{i} ' ' num2str(min(frequencyrange)) '-' num2str(max(frequencyrange)) 'Hz'];
%     end
% end
% [weights,sphere,compvars,bias,signs,lrates,activations] = runica(data(2:5:length(preselection)*5,:),'extended', 5, 'maxsteps',5000);
[weights,sphere,compvars,bias,signs,lrates,activations] = runica(data,'extended', 5, 'maxsteps',5000);
t = tfc.times - 5000;

% [output] = data(7,:);

f = figure;
for i = 1:length(data(:,1))
    h = subplot(5,2,i);
   plot (t, activations(i,:))
   title (['Component ' num2str(i)])
    xlim([-4000 4000]);
    P = get(h, 'Position');
%       %Plot heads in componentplot  
%     newH = axes('Position', [P(1)+(P(3)*.7) P(2)+P(4)/2 P(3)*.3 P(4)/2]);
%     
%     values = weights(:,i);
%     
%     topoplot(values, selectedChanlocs, ...
%         'style','both', ... % just "style: map" doesn't seem to work..
%         'numcontour', 0, ...
%         'electrodes', 'on');
%     set(newH,'clim',[-2 2])
    
    set(h, 'xtick', [-2000 0 2000]);
    set(h, 'xticklabel', {'-2s', '-M-', '2s'});
    set(h, 'xgrid', 'on');
end
reset(f);
% 
% Plot weights
figure;
imagesc(weights, [-1,1])
colorbar; 
% if length(elecSelection) == 1
%     title (['Subject CW - SphereUnambiguousResponse - Electrode P10 - ICA over trials']);
% else title (['Subject CW - SphereUnambiguousResponse - Electrodes P10 and OcO4 - ICA over trials']);
% end
xlabel ('trials'); ylabel ('components');

%% Make individual headplots for each component
% % for i = 1:15
% %         values = weights(:,i);
% %     figure;
% %     topoplot(values, selectedChanlocs, ...
% %         'style','both', ... % just "style: map" doesn't seem to work..
% %         'numcontour', 6, ...
% %         'electrodes', 'labels');
% %     title (['Component ' num2str(i)])
% % end;
% 
return;


% %% DEMO STUFF
% 
% x = 0:500;
% 
% % Good..
% % s1 = sin(x/2);
% % s2 = sin(1+1.2323254*(x/3));
% 
% % Not so good..
% s1 = sin(x/10);
% s2 = sin(1+1.2323254*(x/4));
% s3 = 0.1*sin(x/20);
% s4 = 0.1*cos(x/50);
% s5 = 0.2*cos(x/5);
% 
% r1 = (0.3 * s1) + (-0.4 * s2) + 0.3*s3 + 0.6*s4 + 0.1*s5;
% r2 = (0.7 * s1) + (0.3 * s2) + 0.1*s3 + 0.8*s4  -0.2*s5;
% r3 = (0.2 * s1) + (0.3 * s2) + 0.4*s3 + 0.7*s4 + 0.5*s5;
% r4 = (-0.6 * s1) + (0.4 * s2) + 0.8*s3 + 0.2*s4 + 0.1*s5;
% r5 = (0.1 * s1) + (0.2 * s2) + 0.6*s3 + 0.1*s4 - 0.4*s5;
% clf
% 
% subplot(5,3,1)
% plot(x, s1)
% xlim([min(x) max(x)])
% ylim([-2 2])
% ylabel source1
% 
% subplot(5,3,4)
% plot(x, s2)
% xlim([min(x) max(x)])
% ylim([-2 2])
% ylabel source2
% 
% subplot(5,3,7)
% plot(x, s3)
% xlim([min(x) max(x)])
% ylim([-2 2])
% ylabel source3
% 
% subplot(5,3,10)
% plot(x, s4)
% xlim([min(x) max(x)])
% ylim([-2 2])
% ylabel source4
% 
% subplot(5,3,13)
% plot(x, s5)
% xlim([min(x) max(x)])
% ylim([-2 2])
% ylabel source5
% 
% subplot(5,3,2)
% plot(x, r1)
% xlim([min(x) max(x)])
% ylabel 'input1'
% 
% subplot(5,3,5)
% plot(x, r2)
% xlim([min(x) max(x)])
% ylabel 'input2'
% 
% subplot(5,3,8)
% plot(x, r3)
% xlim([min(x) max(x)])
% ylabel 'input3'
% 
% subplot(5,3,11)
% plot(x, r4)
% xlim([min(x) max(x)])
% ylabel 'input4'
% 
% subplot(5,3,14)
% plot(x, r5)
% xlim([min(x) max(x)])
% ylabel 'input5'
% 
% data = [r1;r2;r3;r4;r5];
% % data = [s1;s2(10:end) s2(1:9)];
% [weights,sphere,compvars,bias,signs,lrates,activations] = runica(data, 'extended', 5);
% comp = weights*sphere*data; % or.. = activations
% 
% subplot(5,3,3)
% plot(x, comp(1,:))
% xlim([min(x) max(x)])
% ylim([-2 2])
% ylabel comp1
% 
% subplot(5,3,6)
% plot(x, comp(2,:))
% ylim([-2 2])
% xlim([min(x) max(x)])
% ylabel comp2
% 
% subplot(5,3,9)
% plot(x, comp(3,:))
% ylim([-2 2])
% xlim([min(x) max(x)])
% ylabel comp3
% 
% subplot(5,3,12)
% plot(x, comp(4,:))
% ylim([-2 2])
% xlim([min(x) max(x)])
% ylabel comp4
% 
% subplot(5,3,15)
% plot(x, comp(5,:))
% ylim([-2 2])
% xlim([min(x) max(x)])
% ylabel comp5